-module(p2).
-compile(export_all).

result() -> main(4000000).

main(Bound) -> lists:sum(filter_even(fibonacci_bounded(Bound))).


filter_even(List) -> [X || X <- List, X rem 2 == 0].

fibonacci_bounded(Bound) -> fibonacci_list([1, 0], Bound).
    
fibonacci_list([F1, F2 | _T]=Z, Bound) when F1 + F2 > Bound -> Z;
fibonacci_list([F1, F2 | _T]=Z, Bound) -> 
    fibonacci_list([F1 + F2 | Z], Bound).
